package com.fr.base.html;

import com.fr.base.CodeUtils;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.html.Tag;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class HTMLPrintWriter
{
  public static final String NEW_LINE = "\n";
  private LinkedList tagChain = new LinkedList();
  private List classes;
  private List styles;
  private PrintWriter writer;
  private boolean debug = false;
  private boolean isLastTagOpened = false;

  public HTMLPrintWriter(PrintWriter paramPrintWriter)
  {
    this.writer = paramPrintWriter;
  }

  public static HTMLPrintWriter create(OutputStream paramOutputStream)
  {
    PrintWriter localPrintWriter;
    try
    {
      localPrintWriter = new PrintWriter(new OutputStreamWriter(paramOutputStream, "UTF8"));
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      localPrintWriter = new PrintWriter(new OutputStreamWriter(paramOutputStream));
    }
    return create(localPrintWriter);
  }

  public static HTMLPrintWriter create(Writer paramWriter)
  {
    return create(new PrintWriter(paramWriter));
  }

  public static HTMLPrintWriter create(PrintWriter paramPrintWriter)
  {
    return new HTMLPrintWriter(paramPrintWriter);
  }

  public HTMLPrintWriter startTAG(String paramString)
  {
    closeLastTagOfChain();
    export("<" + paramString);
    this.tagChain.add(paramString);
    this.isLastTagOpened = true;
    return this;
  }

  public HTMLPrintWriter subHtml(String paramString)
  {
    closeLastTagOfChain();
    export(paramString);
    return this;
  }

  public HTMLPrintWriter subTag(Tag paramTag)
  {
    return subHtml(paramTag.toHtml());
  }

  public HTMLPrintWriter cls(String paramString)
  {
    if (this.classes == null)
      this.classes = new ArrayList();
    this.classes.add(paramString);
    return this;
  }

  public HTMLPrintWriter css(String paramString1, String paramString2)
  {
    if (this.styles == null)
      this.styles = new ArrayList();
    this.styles.add(paramString1 + ":" + paramString2);
    return this;
  }

  private void exportClasses()
  {
    if ((this.classes != null) && (this.classes.size() > 0))
    {
      StringBuffer localStringBuffer = new StringBuffer();
      localStringBuffer.append(" class=\"").append(CodeUtils.htmlEncode(BaseCoreUtils.join(this.classes, " "))).append("\"");
      export(localStringBuffer.toString());
      this.classes.clear();
    }
  }

  private void exportStyles()
  {
    if ((this.styles != null) && (this.styles.size() > 0))
    {
      StringBuffer localStringBuffer = new StringBuffer();
      localStringBuffer.append(" style=\"").append(CodeUtils.htmlEncode(BaseCoreUtils.join(this.styles, ";"))).append("\"");
      export(localStringBuffer.toString());
      this.styles.clear();
    }
  }

  public HTMLPrintWriter attr(String paramString1, String paramString2)
  {
    export(BaseCoreUtils.join(new String[] { " ", paramString1, "=\"", CodeUtils.htmlEncode(paramString2), "\"" }));
    return this;
  }

  private void export(String paramString)
  {
    this.writer.print(paramString);
    if (this.debug)
      System.out.print(paramString);
  }

  private void closeLastTagOfChain()
  {
    if (this.isLastTagOpened)
    {
      exportClasses();
      exportStyles();
      export(">\n");
      this.isLastTagOpened = false;
    }
  }

  public HTMLPrintWriter end()
  {
    String str = (String)this.tagChain.removeLast();
    if (this.isLastTagOpened)
      if ("script".equalsIgnoreCase(str))
      {
        exportClasses();
        exportStyles();
        export("></" + str + ">" + "\n");
      }
      else
      {
        exportClasses();
        exportStyles();
        export("/>\n");
      }
    else
      export("</" + str + ">" + "\n");
    this.isLastTagOpened = false;
    return this;
  }

  public HTMLPrintWriter end(int paramInt)
  {
    while (paramInt-- > 0)
      end();
    return this;
  }

  public void flush()
  {
    this.writer.flush();
  }

  public void close()
  {
    if (this.tagChain.size() > 0)
    {
      String[] arrayOfString = (String[])this.tagChain.toArray(new String[this.tagChain.size()]);
      throw new RuntimeException(BaseCoreUtils.join(new String[] { BaseCoreUtils.join(arrayOfString, ","), (arrayOfString.length > 1) ? "are" : "is", "not closed." }, " "));
    }
    this.writer.close();
  }
}